package AnuncioMVC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


public class ListAnuncios {
	public ArrayList<Anuncio> anuncios=new ArrayList<Anuncio>();
	
	public void getAnuncios(){
		
			Statement stmt=null;
			ResultSet rs=null;
			Connection conn=null;
			long serialVersionUID = 1L;
			// JDBC driver name and database URL
		    String driver="com.mysql.jdbc.Driver";  
		    String db_url="jdbc:mysql://localhost/JAVA";

		    //  Database credentials
		    String User = "root";
		    String Pass = "";
		    try{
				 // Register JDBC driver
		    	Class.forName("com.mysql.jdbc.Driver");
		         // Open a connection
		     	conn = DriverManager.getConnection(db_url,User,Pass);
		     	// Execute SQL query
		         stmt = conn.createStatement();
		         String sql;
		         sql = "SELECT * FROM anuncios;";
		         rs = stmt.executeQuery(sql);

		         // Extract data from result set
		         anuncios.clear();
		         while(rs.next()){
		            //Retrieve by column name
			         Anuncio a=new Anuncio();
			         a.setID(rs.getInt("id"));
			         a.setTitulo(rs.getString("titulo"));
			         a.setTexto(rs.getString("texto"));
			         a.setPrecio(rs.getFloat("precio"));
			         a.setVendido(rs.getBoolean("vendido"));
			         
			         anuncios.add(a);
			        
		           	 
		         } 
		         rs.close();
		         stmt.close();
		         conn.close();
		       
			}catch(SQLException se){
				
		          
			}catch(Exception e){
				
			}finally{
					try {
						
						if(stmt!=null){
							stmt.close();
						}
						
						if(conn!=null){
							conn.close();
						}
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
		    }
		    
		   
		}
		
		
	
}
